package com.twenty48.Servlets;

import com.twenty48.Classes.Game;
import com.twenty48.Classes.GameState;
import com.twenty48.Classes.Score;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewGameServlet extends HttpServlet  {
    public static final long serialVersionID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession(true); // if there's no session object, the one will be created
        session.setMaxInactiveInterval(30*60*60); // inactive lifetime of session object (in seconds)

        // no matter whether we re-write or create new attribute "game" - it should contain new game
        session.setAttribute("game", new Game(GameState.Game));

        Score score = (Score)session.getAttribute("score");
        if(score != null)
           score.setCurrentScore(0);
    }

}
